package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.common.web.model.vo.PageInfoVo;
import com.zhuhjay.service.system.model.vo.PictureVo;
import com.zhuhjay.service.system.service.SystemPictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-05-10
 */
@Api(tags = "图床操作控制器")
@Validated
@RestController
@RequestMapping("/picture")
public class SystemPictureController extends BaseController {

    @Resource
    private SystemPictureService pictureService;

    @ApiOperation("MarkDown图床上传")
    @PostMapping(value = "markdown")
    @PreAuthorize("@perm.hasPerm('user:console:picture:markdown')")
    public String markdownUpload(MultipartFile file) {
        return pictureService.upload(file, "markdown图片");
    }

    @ApiOperation("手动上传")
    @PostMapping(value = "manual")
    @PreAuthorize("@perm.hasPerm('user:console:picture:manual')")
    public String userUpload(MultipartFile file) {
        return pictureService.upload(file, "手动上传图片");
    }

    @ApiOperation("图床分页数据")
    @GetMapping("page/{pageNum}/{pageSize}")
    @PreAuthorize("@perm.hasPerm('user:console:picture:list')")
    public PageInfoVo<PictureVo> page(
            @PathVariable @Range(min = 1, max = Integer.MAX_VALUE) Integer pageNum,
            @PathVariable @Range(min = 5, max = 30) Integer pageSize) {
        return pictureService.picturePage(pageNum, pageSize);
    }

    @ApiOperation("图床图片删除")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("@perm.hasPerm('user:console:picture:remove')")
    public void remove(@PathVariable Long id) {
        pictureService.removePicture(id);
    }

}
